set nocompatible " be iMproved
filetype off " required!
filetype plugin indent on " required!

" 取得本文件所在的目录
let s:home = fnamemodify(resolve(expand('<sfile>:p')), ':h')
" 将 vim-init 目录加入 runtimepath
exec 'set rtp+='.s:home

" 插件配置
call plug#begin(s:home.'/plugged')
"Plug 'skywind3000/vim-auto-popmenu'
Plug 'prabirshrestha/vim-lsp'
Plug 'mattn/vim-lsp-settings'
Plug 'prabirshrestha/asyncomplete.vim'
Plug 'prabirshrestha/asyncomplete-lsp.vim'
Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
call plug#end()

" base
syntax on
set guifont=Consolas:h11
set gfw=courier_new:h11:cGB2312
"set linespace=3 "有时字体会显大，行间距就显得小
set number relativenumber "相对行号

"colo PaperColor
colo gruvbox
"if has('gui_running')
"    set background=light
"else
"    set background=dark
"end
set background=dark

se autochdir
se nobackup
set backspace=indent,eol,start
set hidden
set clipboard=unnamed

se incsearch
se ignorecase
se hlsearch
se autoindent

set expandtab
set shiftwidth=2
set tabstop=2
set softtabstop=2

set encoding=utf-8
set fileencoding=utf-8 
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1

"这个会导致所有的buf都以tab的方式打开，加这行主要是为了拖拽的文件也能以tab的方式打开
"autocmd BufReadPost * tab ball

" 去掉菜单
set go-=m
set go-=T

" 状态栏
set statusline=[%F]%y%r%m%*%=[Line:%l/%L,Column:%c][%p%%]  " 显示文件名：总行数，总的字符数
set ruler " 在编辑过程中，在右下角显示光标位置的状态行

" 设置菜单为英文
set langmenu=en_US
let $LANG='en_US.utf8'  
so $VIMRUNTIME/delmenu.vim
so $VIMRUNTIME/menu.vim

" general
let mapleader = ","
map <space> <C-D>
map <leader>w :w<cr>
map <C-Tab> <C-W>w

"======================================================================================
" apc
"======================================================================================
"let g:apc_enable_ft = {'*':1}
"set cpt=.,k,w,b
"set completeopt=menu,menuone,noselect
"set shortmess+=c


" 自定义函数
function! MyGrep(pattern)
    execute 'vimgrep ' . a:pattern . ' %'
    copen
endfunction
command -nargs=1 MyGrep :call MyGrep(<q-args>)


"======================================================================================
" asyncomplete
"======================================================================================
inoremap <expr> <Tab>   pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
inoremap <expr> <cr>    pumvisible() ? asyncomplete#close_popup() : "\<cr>"


"======================================================================================
" vim-lsp
"======================================================================================
" python
if executable('pylsp')
  " pip install python-lsp-server
  au User lsp_setup call lsp#register_server({
        \ 'name': 'pylsp',
        \ 'cmd': {server_info->['pylsp']},
        \ 'allowlist': ['python'],
        \ })
endif

" settings
function! s:on_lsp_buffer_enabled() abort
  setlocal omnifunc=lsp#complete
  setlocal signcolumn=yes
  if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif
  nmap <buffer> gd <plug>(lsp-definition)
  nmap <buffer> gs <plug>(lsp-document-symbol-search)
  nmap <buffer> gS <plug>(lsp-workspace-symbol-search)
  nmap <buffer> gr <plug>(lsp-references)
  nmap <buffer> gi <plug>(lsp-implementation)
  nmap <buffer> gt <plug>(lsp-type-definition)
  nmap <buffer> <leader>rn <plug>(lsp-rename)
  nmap <buffer> [g <plug>(lsp-previous-diagnostic)
  nmap <buffer> ]g <plug>(lsp-next-diagnostic)
  nmap <buffer> K <plug>(lsp-hover)
  nnoremap <buffer> <expr><c-f> lsp#scroll(+4)
  nnoremap <buffer> <expr><c-d> lsp#scroll(-4)

  let g:lsp_format_sync_timeout = 1000
  autocmd! BufWritePre *.rs,*.go call execute('LspDocumentFormatSync')

  " refer to doc to add more commands
endfunction

augroup lsp_install
  au!
  " call s:on_lsp_buffer_enabled only for languages that has the server registered.
  autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
augroup END


"======================================================================================
" golang
"======================================================================================
let g:go_fmt_command = "goimports" " 格式化将默认的 gofmt 替换
let g:go_autodetect_gopath = 1
let g:go_list_type = "quickfix"

let g:go_version_warning = 1
let g:go_highlight_types = 1
let g:go_highlight_fields = 1
let g:go_highlight_functions = 1
let g:go_highlight_function_calls = 1
let g:go_highlight_operators = 1
let g:go_highlight_extra_types = 1
let g:go_highlight_methods = 1
let g:go_highlight_generate_tags = 1

let g:godef_split=2

let g:ycm_key_list_select_completion = ['<C-n>', '<space>']
let g:ycm_key_list_previous_completion = ['<C-p>', '<Up>']


"======================================================================================
" python
"======================================================================================
let g:python_recommended_style = 0	
autocmd BufEnter *.py set ai sw=2 ts=2 sta et fo=croql
